package com.bmd.backgroundmanger.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.bmd.backgroundmanger.entity.RoleMenu;
import com.bmd.backgroundmanger.mapper.RoleMenuMapper;
import com.bmd.backgroundmanger.service.IRoleMenuService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.swagger.v3.oas.models.security.SecurityScheme.In;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 坤哥
 * @since 2024-07-06
 */
@Service
public class RoleMenuServiceImpl extends ServiceImpl<RoleMenuMapper, RoleMenu> implements IRoleMenuService {
    @Resource
    private RoleMenuMapper roleMenuMapper;

    @Transactional  //事务
    @Override
    public void saveRoleMenuService(RoleMenu roleMenu) {
        //获取角色Id
        int roleId=roleMenu.getRoleId();
        //获取勾选的菜单id
        List<Integer> menuIds=roleMenu.getMenuIds();
        //1.先删除原来的角色对应的菜单
        LambdaQueryWrapper<RoleMenu> wr = new LambdaQueryWrapper<>();
        wr.eq(RoleMenu::getRoleId,roleId);
        roleMenuMapper.delete(wr);
        //2.添加新的角色菜单数据
        for(Integer id:menuIds){
            RoleMenu rm = new RoleMenu();
            rm.setRoleId(roleId);
            rm.setMenuId(id);
            roleMenuMapper.insert(rm);
        }
    }
}
